-- NewInviteListPanel

local NewApplyListRender = import(".renders.NewApplyListRender")

local ClassRef = app.mvc.createPanelClass(...)

local selectedIndexMax = 5 -- 五个标签页之间切换
local pos = nil -- tips初始位置

function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewInviteListNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	pos = cc.p( self._ui["Panel/Panel_tips"]:getPosition() )
	self._ui["Panel/Panel_tips"]:setVisible( false )

	self._curSelectedIndex = 1 -- 默认选中好友
	-- self._ui["Panel/Panel_top/Button_arrow_1"]:addClickEventListener(function()
	-- 		-- 左切
	-- 		if 1 >= self._curSelectedIndex then
	-- 			self._curSelectedIndex = selectedIndexMax
	-- 		else
	-- 			self._curSelectedIndex = self._curSelectedIndex - 1
	-- 		end
	-- 		self:refreshSelectedTag()
	-- 	end)
	-- self._ui["Panel/Panel_top/Button_arrow_2"]:addClickEventListener(function()
	-- 		-- 右切
	-- 		if selectedIndexMax <= self._curSelectedIndex then
	-- 			self._curSelectedIndex = 1
	-- 		else
	-- 			self._curSelectedIndex = self._curSelectedIndex + 1
	-- 		end
	-- 		self:refreshSelectedTag()
	-- 	end)
	for i = 1, selectedIndexMax do
		self._ui["Panel/Panel_top"]:getChildByName( "Button_"..i ):addClickEventListener(function( sender )
			local btnName = sender:getName()
			local selectedIndex = string.sub( btnName, string.len(btnName), string.len(btnName) )
			if tonumber( selectedIndex ) ~= self._curSelectedIndex then
				self._curSelectedIndex = tonumber( selectedIndex )
				self:refreshSelectedTag()
			end
		end)
	end

	self._list = self._ui["Panel/ListView"]
	self._list:removeAllItems()
	self._list.createRender = function (list, idx)
		local items = list:getItems()
		local render = items[idx]
		if nil == render then
			render = NewApplyListRender.create()
			list:pushBackCustomItem(render)
		end
		render:setVisible(true)
		return render
	end

	self:refreshSelectedTag()
end

-- 指定 panel 的指向
function ClassRef:setSelectedTag( idx )
	self._curSelectedIndex = tonumber( idx )
	self:refreshSelectedTag()
end

function ClassRef:refreshSelectedTag()
	for i = 1, selectedIndexMax do
		self._ui["Panel/Panel_top"]:getChildByName( "Button_"..i ):setBright( true )
		self._ui["Panel/Panel_top"]:getChildByName( "name_"..i ):setOpacity( 255 * 0.45 ) -- 45%透明度
	end
	self._ui["Panel/Panel_top"]:getChildByName( "Button_"..self._curSelectedIndex ):setBright( false )
	self._ui["Panel/Panel_top"]:getChildByName( "name_"..self._curSelectedIndex ):setOpacity( 255 )

	-- todo
	if self._onPanelHandler then
		executeFunc(self._onPanelHandler, "InvitePanel_changeTag", self._curSelectedIndex)
	end
end

-- 条件不满足的动画
function ClassRef:playTipsAnimation()
	self._ui["Panel/Panel_tips"]:stopAllActions()

	local actions = {}
	table.insert( actions, cc.CallFunc:create( function()
									self._ui["Panel/Panel_tips"]:setPosition( pos )
									self._ui["Panel/Panel_tips"]:setOpacity( 255 )
									self._ui["Panel/Panel_tips"]:setVisible( true )
								end))

	local flyInFadeTo = cc.FadeTo:create( 1, 0 )
	local flyInMoveTo = cc.MoveTo:create( 1, cc.p( pos.x , pos.y + 50 ) )
	local flyInAction = cc.Spawn:create( flyInFadeTo, flyInMoveTo )

	table.insert( actions, flyInAction )

	table.insert( actions, cc.CallFunc:create( function()
									self._ui["Panel/Panel_tips"]:setVisible( false )
									self._ui["Panel/Panel_tips"]:setPosition( pos )
									self._ui["Panel/Panel_tips"]:setOpacity( 255 )
								end))

	self._ui["Panel/Panel_tips"]:runAction( cc.Sequence:create( actions ) )
end

function ClassRef:registerOnOperate( cb )
	self._onPanelHandler = cb
end

function ClassRef:getListView()
	return self._list
end

function ClassRef:remove()
end

return ClassRef
